package com.boot.unity.api.wx.service;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.boot.unity.api.wx.utils.WxOcrIdCardResults;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Service
public class WxOcrService {
    private final Log logger = LogFactory.getLog(WxOcrService.class);

    private final WxMaService mainService;

    public WxOcrService(WxMaService mainService) {
        this.mainService = mainService;
    }

    public WxOcrIdCardResults idCard(String imgUrl) throws WxErrorException {
        try {
            imgUrl = URLEncoder.encode(imgUrl, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException var3) {
        }

         System.out.println(imgUrl);

        String result = this.mainService.post(String.format("https://api.weixin.qq.com/cv/ocr/idcard?img_url=%s", imgUrl), (String)null);
        return WxOcrIdCardResults.fromJson(result);
    }

}
